package com.example.yyj.Controller;


import com.example.yyj.Entity.ResultEntity;
import com.example.yyj.Entity.User;
import com.example.yyj.Service.UserService;
import com.example.yyj.Util.JSONTool;
import com.example.yyj.aop.CheckToken;
import com.example.yyj.aop.PasToken;
import com.example.yyj.tool.EmailTool;
import com.example.yyj.tool.JsonUtil;
import com.example.yyj.tool.RedisTool;
import com.example.yyj.tool.TokenTool;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/User")
public class UserController {
    ResultEntity resultEntity=null;
    @Resource
    UserService userService;
    @Resource
    RedisTool redisTool;
    @Resource
    EmailTool emailTool;
    @Resource
    TokenTool tokenTool;

    @PasToken//不需要接口校验
    @GetMapping("PasToken")
    private  String PasToken(){
        return "pass";
    }

    @CheckToken//需要接口校验
    @GetMapping("CheckToken")
    private  String CheckToken(){
        return "nopass";
    }

    @PostMapping("UserLogin")//账号密码登录
    private  String doLogin(
            @RequestParam("Number")String Number,
            @RequestParam("Pwd") String Pwd
    ){
        resultEntity=new ResultEntity();
        try {
            User userEntity= userService.login(Number,Pwd);
            if (userEntity==null || userEntity.getUName().equals("")){
                resultEntity.setCode(2);
                resultEntity.setMsg("用户名或密码错误");
            }else {
                resultEntity.setCode(0);
                resultEntity.setMsg("登录成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(1);
            resultEntity.setMsg("登录失败");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);//返回JSON格式文件
    }

    @GetMapping("fingerprintLogin")//指纹登录准备
    private  String fingerprintLogin(@RequestParam("fingerprintLogin") String fingerprintLogin){
        resultEntity = new ResultEntity();
        try {
           User user=userService.fingerprintLogin(fingerprintLogin);
           if (user !=null){
               resultEntity.setCode(0);
               resultEntity.setData(user);
           }else {
               resultEntity.setCode(1);

           }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(999);
            resultEntity.setMsg("系统错误联系管理员");

        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

@GetMapping("Redis")
    private String Redis(@RequestParam("val") String val){
    Jedis jedis=redisTool.getJedis();
    if (jedis.exists("2020080128")){
        return "key存在";
    }else {
        jedis.set("2020080128",val);
        jedis.expire("2020080128",60);
    }
    System.out.println(jedis.get("2020080128"));
    return "success";
}
    @PasToken//不需要接口校验
    @GetMapping("SendCode")
    private String SendCode(@RequestParam("EmailAddress") String EmailAddress){
        resultEntity=new ResultEntity();
        Jedis jedis=redisTool.getJedis();
        try{
            if (userService.getUserByEmail(EmailAddress)==null||userService.getUserByEmail(EmailAddress).getEmailAddress().equals("")){
                resultEntity.setCode(3);
                resultEntity.setMsg("用户不存在");
            }else {
                if (jedis.exists(EmailAddress)){
                    resultEntity.setCode(1);
                    resultEntity.setMsg("请不要频繁点击");
                }else {
                    int Rad=0;
                    Rad=(int) (Math.random()*8999)+1000;
                    String MesCode="不要说出去 你的验证码是"+Rad+".60秒内有效果";
                    emailTool.sendMail(EmailAddress,"邮箱验证码",MesCode,null);
                    jedis.set(EmailAddress,String.valueOf(Rad));
                    jedis.expire(EmailAddress,60);
                    resultEntity.setCode(0);
                    resultEntity.setMsg("发送成功");

                    }
                }

        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(9999);
            resultEntity.setMsg("系统错误请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
@PostMapping("SendCode")
    private String IsCode(
            @RequestParam("EmailAddress") String EmailAddress,
            @RequestParam("Code") String Code
){
    resultEntity=new ResultEntity();
    try {
        User user=userService.getUserByEmail(EmailAddress);
        Jedis jedis=redisTool.getJedis();
        if (jedis.exists(EmailAddress)){
            if (jedis.get(EmailAddress).equals(Code)){
                resultEntity.setCode(0);
                resultEntity.setMsg("对比成功");
                resultEntity.setData(user);
                resultEntity.setCount(tokenTool.getToken(EmailAddress));
            }else {
                resultEntity.setCode(1);
                resultEntity.setMsg("对比失败");
            }
        }else {
            resultEntity.setCode(2);
            resultEntity.setMsg("key不存在");
        }
    }catch (Exception e){
        e.printStackTrace();
        resultEntity.setCode(99999);
        resultEntity.setMsg("系统错误 请联系管理员");
    }
    return JsonUtil.getJsonTool().toJson(resultEntity);
}

}
